package com.fastdfs.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 胡安民
 * 时间:  2020 /12 /7
 */
public class Page<T> implements Serializable{

	// 当前第几页
	private Integer  current_Page;
	//每页多少条
	private Integer  page_Size;

	// 一共多少条数据
	private Integer   data_Total_Seize;

	//一共多少页
	private  Integer  page_Total__Seize;

	//  上一页
	private Integer page_Up;

	// 下一页
	private Integer page_Dn;

	//当前页的 数据
	private List<T> list;

	//第一页
	private Integer start_Page=1;

	// 最后一页
	private Integer page_End;

	/****
	 *
	 * @param
	 * @param current_Page 当前第几页
	 * @param page_Size 每页多少条
	 */
	public Page(int current_Page, int page_Size,List<T> list) throws Exception {
		if(current_Page==0){
			current_Page=1;
		}

		//总条数
		int size = list.size();
		if (size==0){
			throw new Exception("没有分页的数据");
		}


		this.data_Total_Seize=size ;
			//每页显示的条数
		this.page_Size= page_Size(page_Size,data_Total_Seize);

		this.page_Total__Seize=page_Total__Seize(size,this.page_Size);

		this.page_End=this.page_Total__Seize;
		this.current_Page=current_Page(current_Page,this.page_End);
		this.page_Up=page_Up(this.current_Page,this.start_Page);
		this.page_Dn= page_Dn(this.current_Page,this.page_End);
		this.list=list(list);

	}

	// 判断当前显示的条数不能大于总条数

	private  Integer  page_Size(int page_Size,int data_Total_Seize){

		if (page_Size>=data_Total_Seize){
			return  data_Total_Seize;
		}else {
			return page_Size;
		}

	}

	//设置当前页如果大于总 页数那么就以最后一页为准
	private  Integer current_Page(int current_Page,Integer page_End){

		if (current_Page>=page_End){
			return  page_End;
		}else {
			return current_Page;
		}
	}

	//获取上一页的条数     如果上一页小于初始页  那么 就以初始页为准
	private  Integer page_Up( int current_Page,Integer start_Page){

		if (current_Page<=start_Page){
			return  current_Page;
		}else {
			return current_Page-1;
		}


	}

	//获取下一页的条数   如果下一页大于最后页 那么就以当前页为准
	private  Integer page_Dn( int current_Page,Integer page_End){


		if (current_Page>=page_End){
			return  current_Page;
		}else {
			return current_Page+1;
		}

	}

	// 获取总页数
	private Integer page_Total__Seize( Integer data_Total_Seize,int page_Size){
		if(data_Total_Seize%page_Size==0){

			return  data_Total_Seize/page_Size;
		}else{
			return (data_Total_Seize/page_Size)+1;
		}
	}

	//获取当前页的数据
	private List<T> list(List<T> list){
		List<T> s=new ArrayList<>();

		int tiaos=this.page_Size;
		//如果要查询的条数 超出 剩余 的条数
		boolean b = (this.data_Total_Seize-( this.current_Page * this.page_Size )< 0);
		// 那么 只查询 到的剩余条数
		if (b){

			tiaos= this.page_Size-Math.abs((this.data_Total_Seize-( this.current_Page * this.page_Size )));
		}

		for (int i = ((this.current_Page-1)*this.page_Size),j=0; j < tiaos;j++, i++) {
			s.add(list.get(i));
		}
		return  s;
	}


	public Integer getCurrent_Page() {
		return current_Page;
	}

	public void setCurrent_Page(Integer current_Page) {
		this.current_Page = current_Page;
	}

	public Integer getPage_Size() {
		return page_Size;
	}

	public void setPage_Size(Integer page_Size) {
		this.page_Size = page_Size;
	}

	public Integer getData_Total_Seize() {
		return data_Total_Seize;
	}

	// 改变总条数 从而改变  配置信息     从构造里传入的list数据不变
	public void setData_Total_Seize(Integer data_Total_Seize) {
		//总条数
		this.data_Total_Seize = data_Total_Seize;
		if(current_Page==0){
			current_Page=1;
		}
		//每页显示的条数
		this.page_Size= page_Size(page_Size,data_Total_Seize);
		this.page_Total__Seize=page_Total__Seize(data_Total_Seize,this.page_Size);
		this.page_End=this.page_Total__Seize;
		this.current_Page=current_Page(current_Page,this.page_End);
		this.page_Up=page_Up(this.current_Page,this.start_Page);
		this.page_Dn= page_Dn(this.current_Page,this.page_End);
	}

	public Integer getPage_Total__Seize() {
		return page_Total__Seize;
	}

	public void setPage_Total__Seize(Integer page_Total__Seize) {
		this.page_Total__Seize = page_Total__Seize;
	}

	public Integer getPage_Up() {
		return page_Up;
	}

	public void setPage_Up(Integer page_Up) {
		this.page_Up = page_Up;
	}

	public Integer getPage_Dn() {
		return page_Dn;
	}

	public void setPage_Dn(Integer page_Dn) {
		this.page_Dn = page_Dn;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) throws Exception {

		this.list = list;
	}

	public Integer getStart_Page() {
		return start_Page;
	}

	public void setStart_Page(Integer start_Page) {
		this.start_Page = start_Page;
	}

	public Integer getPage_End() {
		return page_End;
	}

	public void setPage_End(Integer page_End) {
		this.page_End = page_End;
	}


	@Override
	public String toString() {
		return "Page{" +
				"\t开始页(start_Page):"+getStart_Page()+
				"\t\t上一页(page_Up):"+getPage_Up()+
				"\t\t当前页(current_Page):"+getCurrent_Page()+
				"\t\t下一页(page_Dn):"+page_Dn+
				"\t\t每页(page_Size):"+page_Size+"条数据"+
				"\t\t最后一页(page_End):"+getPage_End()+
				"\t\t一共(data_Total_Seize)"+data_Total_Seize+"条数据"+
				"\t\t总页数(page_Total__Seize)："+page_Total__Seize+
				"\n当前页数据(list):"+getList()+
				"}";
	}

	/**
	 * 测试
	 * @param args
	 */

	public static void main(String[] args) throws Exception {

		List<String> data=new ArrayList<>();
		data.add("1");
		data.add("2");
		data.add("3");
		data.add("4");
		data.add("5");
		data.add("6");
		data.add("7");
		data.add("8");
		data.add("9");
		data.add("10");
		data.add("11");

		//当前页
		int currentpage =1;
		//每页显示多少条
		int pagesize=3;
		//当前页		每页多少条   数据
		Page<String> page = new Page<>(currentpage, pagesize,data);

		System.out.println(page);

	}


}
